home *** CD-ROM | disk | FTP | other *** search
/ Over 1,000 Windows 95 Programs / Over 1000 Windows 95 Programs (Microforum) (Disc 1).iso / 1459 / rdel.cpp next >
C/C++ Source or Header  |  1995-08-30  |  1KB  |  68 lines

  1. /*
  2.  * Program: rdel V1.0
  3.  * Author:  Rik Wade (olds@scs.leeds.ac.uk)
  4.  * Date:    30/08/1995
  5.  *
  6.  * Comments: 'rdel' is a little hack for those who want files which
  7.  *           are deleted at the command prompt to be placed in the
  8.  *           'recycle' bin on that drive.
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <dir.h>
  15.  
  16. /* This is the directory where all recycled files go
  17.  */
  18. #define DELDIR "recycled"
  19.  
  20.  
  21. void main(int argc, char* argv[])
  22. {
  23.     /* For the file copying
  24.      */
  25.     FILE *infile, *outfile;
  26.  
  27.     char *dname = DELDIR, *fname = "\0", *ptr;
  28.     char buf[10];
  29.  
  30.     /* Make sure we get a single filename
  31.      */
  32.     if (argc!=2) { printf("usage: rdel <filename>\n"); exit(1); }
  33.  
  34.     ptr = strrchr(argv[1],'\\');
  35.     if (!ptr) { ptr = argv[1]; }
  36.  
  37.     strcat(fname,"\\");
  38.     strcat(fname,dname);
  39.     strcat(fname,"\\");
  40.     strcat(fname,ptr);
  41.  
  42.     /* If the file opens then process it, otherwise nothing
  43.      */
  44.     if ( (!(infile = fopen(argv[1], "rb"))) ||
  45.         (!(outfile = fopen(fname, "wb"))) ) {
  46.  
  47.            perror("Unable to open file");
  48.     }
  49.     else
  50.     {
  51.         printf("Recycling: %s\n",argv[1]);
  52.  
  53.         /* Now read a byte, write a byte.
  54.          */
  55.         while(!feof(infile)) {
  56.             if(fread(buf,1,1,infile)) {
  57.                 fwrite(buf,1,1,outfile);
  58.             }
  59.         }
  60.         fclose(infile);
  61.         fclose(outfile);
  62.  
  63.         /* Remove the original file
  64.          */
  65.         remove(argv[1]);
  66.     }
  67. }
  68.